#!/usr/bin/env python
# encoding: utf-8
"""
@author:zwb
@file: pages.py
@contact: zwb15083976291@163.com
@time: 2021/12/06
"""
from rest_framework import serializers

from common.models import BlogPages
from utils.func import whether_url
from utils.serializers import SpecifySerializeFieldModelSerializer
from utils.serializers.serializer import UpdateBaseSerializeMethodMixin


class PagesSerializer(UpdateBaseSerializeMethodMixin, SpecifySerializeFieldModelSerializer):
    id = serializers.IntegerField()

    class Meta:
        model = BlogPages
        fields = '__all__'

    def validate_background_image_url(self, value):
        if whether_url(value):
            return value.strip()
        raise serializers.ValidationError("请输入合法的URL")

    def validate_id(self, id):
        if BlogPages.objects.filter(id=id).first():
            return id
        raise serializers.ValidationError("该数据不存在")
